define(['Util',
        'tab',
        'cookie',
        'text!tpl/myProject/menu.tpl',
        'dialog',
        '../common/getPath',
        '../common/getQueryString',
    ],
    function(Util, Tab, $cookie, omenutpl, Dialog, AjaxPath, GetQueryString) {
        var tabList = null;
        var options = null;
        var projectMenuId = null;
        var bindMenuClick = function(menuObj){
            menuObj.click(function(e) {
                e.stopPropagation();
                var dataId = $(this).attr('data-id');
                var dataUrl = $(this).attr('data-url');
                var dataType = $(this).attr('data-type');
                var tabName = $(this).text();
                if(dataUrl === null || dataUrl === '') {
                    return;
                }
                //门户上点击平台管理 进入项目列表 在通过菜单点击项目列表bug
                if(dataId === "4"){
                    dataId = "project";
                }
                require([dataUrl], function(objClass) {

                    /*tabList.createTab({
                        title: tabName,
                        closeable: 1, //是否可以关闭 0false不可以|1true可以
                        className: 'addProjectTab'
                    });
                    tabList.switchTab(tabName);
                    var projectdata = options;
                    projectdata.menuId = dataId;
                    require([dataUrl], function(addProjectContent) {
                        var result = new addProjectContent({}, projectdata, tabList);
                        tabList.content(result.content);
                    });*/
                    var isSwitchFlag = true;
                    var tabs = tabList.options.tabs;
                    for(var i = 0; i < tabs.length; i++) {
                        if(tabs[i].className === dataId) {
                            var float = $('.' + dataId).position().left;
                            if(float < 0 || float > 1000) {
                                $('.sn-tab-items').animate({
                                    'scrollLeft': float + 'px'
                                }, 200);
                            }
                            tabList.switchTab(dataId);
                            isSwitchFlag = false;
                            break;
                        }
                    }
                    if(isSwitchFlag) {
                        tabList.createTab({
                            title: '' + tabName,
                            className: dataId,
                            closeable: 1
                        });

                        _objClass = objClass;
                        // console.log("当前js模块:"+dataUrl+".类型是:"+typeof(_objClass));
                        if(typeof(_objClass) === 'function') {
                            var result = new _objClass({}, {
                                menuId: dataId,
                                menuName: tabName,
                                menuUrl: dataUrl,
                                menuType: dataType
                            }, tabList);
                            if(typeof(result) === 'object') {
                                if(result.hasOwnProperty('content')) {
                                    setTimeout(tabList.content(result.content), 200);
                                } else {
                                    setTimeout(tabList.content(result), 200);
                                }
                            } else {
                                setTimetout(tabList.content(result), 200);
                            }
                        }

                        $('.i_panel_left').css('height', $(document.body).height() - 90);
                        $('.i_panel_right').css('height', $(document.body).height() - 90);
                        $(window).resize(function() {
                            $('.i_panel_left').css('height', $(document.body).height() - 90);
                            $('.i_panel_right').css('height', $(document.body).height() - 90);
                        });
                    }
                    
                }, function() {

                });
            });
        };
        var getMenu = function(Tab, optId){
            console.log(332,optId)
            tabList = Tab;
            // if(opt === null || opt === undefined){
            //     options = {};
            // }else {
            //     options = opt; 
            // }
            //权限
            // Util.ajax.getJson(AjaxPath.auth + '/authority/button/'+ $cookie.get("userId") +'?datas=[{"mo":"21","btnId":""}]', {}, function(result, json){
                // if(){
                    //本地菜单
                    // Util.ajax.postJson('/data/menu.json', {}, function(json, status) {
                    //     for(var i=0;i<json.beans.length;i++){
                    //         if(json.beans[i].menuName === "平台管理"){
                    //             json.beans.splice(i,1);
                    //         }
                    //     }
                    //     console.log(21,json);
                    //     $(".menuStore").html( Util.hdb.compile(omenutpl)(json) );
                    //     $(".myMenu li").has("ul").mouseover(function () {
                    //         $(this).children("ul").css("display", "block");
                    //         $(this).css("backgroundColor", "#63a8eb");
                    //     }).mouseout(function () {
                    //         $(this).children("ul").css("display", "none");
                    //         $(this).css("backgroundColor", "#eee");
                    //     });
                    //     bindMenuClick($(".myMenu li ul a")); 
                    // });
                // } else {
                    if($cookie.get("projectSelectId") === null || $cookie.get("projectSelectId") === undefined){
                        projectMenuId = 0;
                    }else{
                        projectMenuId = $cookie.get("projectSelectId");
                    }
                    if(optId === "project"){
                        optId = "4";
                    }
                    Util.ajax.getJson(AjaxPath.project + '/resources/findTreeById/'+ optId + "/" + projectMenuId, {}, function(dataReturn, dataStatus) {
                        Util.ajax.getJson(AjaxPath.project + '/resources?userId='+$cookie.get("userId")+'&dataStructure=tree', {}, function(json, status) {
                            // console.log(json, Util.hdb.compile(omenutpl)(json))
                            json.crumbs = dataReturn.beans;
                            if(json.crumbs.menuId === 14){
                                json.JudgeMenu = false;
                            }else{
                                json.JudgeMenu = true;
                            }
                            // json.crumbs.projectName = $cookie.get("projectSelectId");
                            $(".menuStore").html( Util.hdb.compile(omenutpl)(json) );
                            $(".myMenu li").has("ul").mouseover(function () {
                                $(this).children("ul").css("display", "block").children("li").mouseover(function(){
                                    $(this).css("backgroundColor", "#63a8eb");
                                }).mouseout(function(){
                                    $(this).css("backgroundColor", "#eee");
                                });
                                $(this).css("backgroundColor", "#63a8eb");
                            }).mouseout(function () {
                                $(this).children("ul").css("display", "none");
                                $(this).css("backgroundColor", "#eee");
                            });
                            bindMenuClick($(".myMenu li ul a")); 
                        });
                    });
                // }
            // });
        };
	return getMenu;
})
